<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Feedback;
use App\Helpers\FunTool;
use App\Services\Feedback\FeedbackService;
use App\Services\Tenancy\Tenancy;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;
use Illuminate\Support\Str;

class FeedbackController extends AdminController
{
    protected $title="投诉列表";
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Feedback(['userInfo','jobPositionInfo']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('jobPositionInfo.id','职位ID');
            $grid->column('jobPositionInfo.position_name','职位名称');
            $grid->column('userInfo.name','用户姓名');
            $grid->column('userInfo.mobile','用户手机号');
            $grid->column('content','内容')->display('查看')// 设置按钮名称
            ->expand(function (Grid\Displayers\Expand $expand){
                // 设置按钮名称
                $expand->button('详情');
                $card = new Card();
                // 设置间距
                $card->padding("10px");
//                $content = "<div style='padding:10px 10px 0;line-height: 35px;'>问题描述： $this->title </div>";
                $content = "<div style='padding:10px 10px 0;line-height: 35px;'>内容： $this->content </div>";
                $pics = "";
                if($this->feed_pic){
                    foreach ($this->feed_pic as $value){
                        $pics.='<p style="margin-right: 5px;margin-top: 10px;display: inline-block"><img data-action="preview-img" src="'. $value.'" style="max-width:100px;max-height:100px;cursor:pointer" class="img img-thumbnail"></p>';
                    }
                }
                $response="";
                if(!empty($this->response)){
                    $response = "<div style='padding:10px 10px 0;line-height: 35px;'>回复内容： $this->response </div>";
                }
                return $content.$pics.$response;
            });
            $grid->column('scene','问题分类')->using(FeedbackService::FEEDBACK_SCENE);
            $grid->column('created_at','提交时间');
            $grid->filter(function (Grid\Filter $filter) {
                // 展开过滤器
                $filter->expand();
                $filter->panel();
                // 注意切换为panel布局方式时需要重新调整表单字段的宽度
                $filter->equal('userInfo.mobile','手机号')->width(2);
            });
            $grid->actions(function (Grid\Displayers\Actions $actions){
               $actions->quickEdit();
            });
            $grid->disableActions();
            $grid->disableRowSelector();
            //禁用新增按钮
            $grid->disableCreateButton();
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            // 禁用批量删除按钮
            $grid->disableBatchDelete();
            // 禁用显示按钮
            $grid->disableViewButton();
            // 去掉删除
            $grid->disableDeleteButton();
            $grid->disableEditButton();
            //显示横向滚动条
            $grid->scrollbarX();
        });
    }



    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Feedback(), function (Form $form) {
            $form->display('account');
            $form->display('title');
            $form->display('content');
            $form->multipleImage('feed_pic','问题图片')->customFormat(function ($value){
                if(isset($value[0]) && Str::contains($value[0],'|')){
                    return explode('|',$value[0]);
                }
                return $value;
            })->disable();
            $form->hidden('cur_status');

            $form->saving(function (Form $form){
                if ($form->model()->cur_status == 0){
                    $form->cur_status=1;
                }
                $form->deleteInput('feed_pic');
            });
            $form->textarea('response','回复');
            $form->display('created_at','留言日期');
        });
    }
}
